
export function formatDate(date) {
	var date = new Date(date);
	var YY = date.getFullYear() + '-';
	var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
	var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
	var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
	var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
	var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
	return YY + MM + DD +" "+hh + mm + ss;
}

export function Console(width,height,command){
	let container=document.createElement("div");
	this.container=container;
	container.style.width=(width);
	container.style.height=(height);
	container.style.margin="0px";
	container.style.fontFamily="Terminal";
	container.style.fontSize="16px";
	this.output=document.createElement("div");
	container.appendChild(this.output);
	// this.input=document.createElement("input");
	// container.appendChild(this.input);
	// this.input.style.borderWidth="0px";
	// this.input.style.fontFamily="Terminal";
	// this.input.style.width="90%"
	// this.input.style.fontSize="16px"
	// this.input.style.position="relative";
	// this.input.style.top="2px";
	command=command||function(str)
	{
		let e;
		let r;
		let util = require("util")
		let overObject = function(key,value,first){
			let ele = document.createElement("div");
			ele.classList.add("console-line");
			if(first){
				ele.innerHTML = `<str>${key}  ${util.inspect(value)}</str>`;
			} else {
				ele.innerHTML = `<str-child><key>${key}</key>  ->  <value>${util.inspect(value)}</value></str-child>`;
			}
			ele.isextend = false;
			ele.childSigns = new Array();
			ele.onclick = function(e){
				e.stopPropagation();
				if(e.target.localName=="str"||e.target.parentNode.localName=="str-child"){}else{return;}
				if (this.isextend){
					this.isextend = false;
					this.childSigns.forEach(element => {
						element.remove();
					});
					return;
				};
				this.isextend = true;
				Object.keys(value).forEach(key1 => {
					let child = this.cloneNode(true);
					if(value[key1]&&value[key1] instanceof Object){
						child = overObject(key1,value[key1],false);
					} else {
						child.innerHTML = `<font><key>${key1}</key>:  <value>${value[key1]}</value></font>`;
					}
					this.appendChild(child); //唯一添加子元素
					this.childSigns.push(child);
				});
			};
			return ele;
		}
		try{
			r=eval(str);
		} catch(e) {
			return "Bad command";
		}
		return overObject(str,r,true);

	}
	this.command=function(query,val){
		let value = val||this.value;
		let com = query.command||value
		console.log(this,this.parentNode);
		this.container.innerHTML+=`${query.from}-><text>${com}</text><br/>`
		// this.parentNode.childNodes[0].innerHTML+=(command(this.value)+"<br/>")
		let child = command(value);
		child.innerHTML += '<br/>'
		this.container.appendChild(child)
	}
	// this.input.onkeyup=new Function("e","e=e||event;if(e.keyCode!=13)return;this.command({from:'js'},null);this.value='';");
	this.appendto=function(parent)
	{
		parent.appendChild(this.container);
	}
	this.clearContent = function(){
		this.container.innerHTML = "";
	}
	container=null;
}

export function  actApi(){
        var first = document.body.firstChild;
        var oScript = document.createElement("script");
        oScript.src = "http://pv.sohu.com/cityjson?ie=utf-8&callback=returnCitySN&t=" + new Date().getTime();
        document.body.insertBefore(oScript, first);
        window.onload = () => {
            let nScript = document.createElement("script");
            nScript.src = "http://ip.ws.126.net/ipquery?ip="+returnCitySN.cip;
            document.body.insertBefore(nScript, first);
            nScript.onload = function(e){
                console.log(top.localAddress);
                that.info.localAddress = top.localAddress.province+" "+top.localAddress.city;
            }
            document.body.removeChild(oScript);
            document.body.removeChild(nScript);
        }
}
